/**
 * 
 */
package com.ideeli.gextractor.service;

import static com.ideeli.webextractor.utils.Constants.BASE_URL;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.ideeli.webextractor.domain.ProductDetail;

/**
 * @author spaida
 *
 */
public class GExtractImpl implements GExtract{
	
	private static final Logger logger = Logger.getLogger(GExtractImpl.class);
	
	private WebDriver driver;
	
	private Properties gProps;
	
	public GExtractImpl() throws Exception
	{
		gProps= new Properties();
		gProps.load(new FileInputStream("prop/g-ext.properties"));
		
	}

	@Override
	public String extract(ProductDetail prodDetail) throws Exception 
	{
		initBrowser();
		
		driver.findElement(By.id("gbqfq")).clear();
		driver.findElement(By.id("gbqfq")).sendKeys("1025C-WHI067S");
		driver.findElement(By.id("gbqfb")).click();
		
		String str=driver.getPageSource();
		logger.warn("str:"+str);
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
//		driver.findElement(By.cssSelector("div.vsc.vso > div.vspib > div.vspii > div.vspiic")).click();
		
		return null;
	}
	
	private void initBrowser()
	{
		if(driver!=null)
			return;
		
		logger.warn("initBrowser()..");
//		File fExeFile = new File("C:\\Users\\spaida\\prog\\firefox.exe");
		File fExeFile = new File("C:\\Users\\spaida\\c\\f12\\firefox.exe");
		                            
		if(fExeFile.exists())
		{
			
			FirefoxBinary fireFoxBinary = new FirefoxBinary(fExeFile);
			driver = new FirefoxDriver(fireFoxBinary, null);
//			DesiredCapabilities capabilities =DesiredCapabilities.internetExplorer();
//			capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 
//			driver = new InternetExplorerDriver(capabilities);

//			driver = new InternetExplorerDriver(); 
		}
		else
		{
//			File foxProfile= new File("E:\\Users\\sa\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\0duwwsr0.default");
//			driver = new FirefoxDriver(new FirefoxProfile(foxProfile));
			driver = new FirefoxDriver();
		}
	
		String baseUrl = gProps.getProperty(BASE_URL);
	
//		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		logger.info("base url..");
		driver.get(baseUrl + "/");
		
//		driver.quit();
		
		
//		driver.get(baseUrl + "/");
//
//		logger.info("Send username..");
//		driver.findElement(By.id("username")).clear();
//		driver.findElement(By.id("username")).sendKeys(gProps.getProperty(USER));
//		logger.info("Send password..");
//		driver.findElement(By.id("password")).clear();
//		driver.findElement(By.id("password")).sendKeys(gProps.getProperty(PASS));
//		logger.info("click login..");
//		driver.findElement(By.id("loginBtn")).click();

		
	}


	
	
}
